//
//  MWModifyEmailController.m
//  XiRan-iOS
//
//  Created by liuyiming on 2019/10/31.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWModifyEmailController.h"
#import "JKCountDownButton.h"

@interface MWModifyEmailController ()

@property(nonatomic, strong) UILabel *hintLB;
@property(nonatomic, strong) UITextField *emailTF;
@property(nonatomic, strong) UIView *emailLineView;

@property(nonatomic, strong) UITextField *smsCodeTF;
@property(nonatomic, strong) JKCountDownButton *countDownButton;
@property(nonatomic, strong) UIView *smsCodeLineView;

@property(nonatomic, strong) UIButton *submitBtn;

@end

@implementation MWModifyEmailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [[MWConfigManager manager] objectForKey:@"reviseemail" defaultValue:@"完善邮箱地址"];
    if ([MWConfigManager manager].isNetError) {
        self.errorTitleLabel.text = [[MWConfigManager manager] objectForKey:@"click_retry" defaultValue:@"点击重试"];
        self.errorTitleLabel.hidden = self.errorImageView.hidden = NO;
    } else {
        [self setupPageSubviews];
        [self layoutPageSubviews];
    }
}

- (void)refresh {
    [super refresh];
    if ([MWConfigManager manager].isNetError) {
        self.errorTitleLabel.hidden = self.errorImageView.hidden = NO;
    } else {
        self.errorTitleLabel.hidden = self.errorImageView.hidden = YES;
        [self setupPageSubviews];
        [self layoutPageSubviews];
    }
}


- (void)submitAction:(UIButton *)sender {
    NSString *email = _emailTF.text;
    NSString *captcha = _smsCodeTF.text;
    [self modifyeEmail:email captcha:captcha];
}

#pragma mark - 修改邮箱验证码
- (void)loadCodeWithEmail:(NSString *)email {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:email forKey:@"email"];
    [paraDic setObject:@"changeemail" forKey:@"event"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
     MWWeakSelf
    [[MWNetworkManager manager] POST:kEmailCodeUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = msg;
        [hud hideAnimated:YES afterDelay:2];
        
        if (code == 1) {
            weakSelf.countDownButton.enabled = NO;
            [weakSelf.countDownButton startCountDownWithSecond:60];
        }
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - 修改邮箱
- (void)modifyeEmail:(NSString *)email captcha:(NSString *)captcha {
    NSString *token = [MWUserManager manager].user.token;
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:token forKey:@"token"];
    [paraDic setObject:email forKey:@"email"];
    [paraDic setObject:captcha forKey:@"captcha"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.label.text = [[MWConfigManager manager] objectForKey:@"loadin" defaultValue:@"加载中..."];
    
    MWWeakSelf
    [[MWNetworkManager manager] POST:kModifyEmailUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        
        hud.mode = MBProgressHUDModeText;
        hud.label.text = msg;
        [hud hideAnimated:YES afterDelay:2];
        
        if (code == 1) {
            [MWUserManager manager].user.email = weakSelf.emailTF.text;
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    } failure:^(NSError *error) {
        [hud hideAnimated:YES];
    }];
}

#pragma mark - UI
- (void)setupPageSubviews {
    _hintLB = [[UILabel alloc] init];
    _hintLB.font = MWBoldFont(14);
    _hintLB.textColor = MWHexColor(@"#333333");
    _hintLB.text = [[MWConfigManager manager] objectForKey:@"input_new_email" defaultValue:@"请输入新的邮箱地址"];
    [self.view addSubview:_hintLB];
    
    _emailTF = [[UITextField alloc] init];
    _emailTF.font = MWFont(12);
    _emailTF.textColor = MWHexColor(@"#BAC2CE");
    _emailTF.placeholder = [[MWConfigManager manager] objectForKey:@"input_email" defaultValue:@"请输入邮箱地址"];
    _emailTF.text = [MWUserManager manager].user.email;
    [self.view addSubview:_emailTF];
    
    _emailLineView = [[UIView alloc] init];
    _emailLineView.backgroundColor = MWHexColor(@"#D8D8D8");
    [self.view addSubview:_emailLineView];
    
    _smsCodeTF = [[UITextField alloc] init];
    _smsCodeTF.font = MWFont(12);
    _smsCodeTF.textColor = MWHexColor(@"#BAC2CE");
    _smsCodeTF.placeholder = [[MWConfigManager manager] objectForKey:@"input_code" defaultValue:@"请输入验证码"];
    [self.view addSubview:_smsCodeTF];
    
    _smsCodeLineView = [[UIView alloc] init];
    _smsCodeLineView.backgroundColor = MWHexColor(@"#D8D8D8");
    [self.view addSubview:_smsCodeLineView];
    
    _countDownButton = [JKCountDownButton buttonWithType:UIButtonTypeCustom];
    _countDownButton.titleLabel.font = MWFont(10);
    [_countDownButton setTitle:[[MWConfigManager manager] objectForKey:@"get_code" defaultValue:@"获取验证码"] forState:UIControlStateNormal];
    [_countDownButton setTitleColor:MWHexColor(@"#1E1934") forState:UIControlStateNormal];
    [self.view addSubview:_countDownButton];
    
    MWWeakSelf
    [_countDownButton countDownButtonHandler:^(JKCountDownButton*sender, NSInteger tag) {
        [self loadCodeWithEmail:weakSelf.emailTF.text];
        
        [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
            NSString *title = [NSString stringWithFormat:@"%zds",second];
            return title;
        }];
        
        [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
            countDownButton.enabled = YES;
            return [[MWConfigManager manager] objectForKey:@"getcode_retry" defaultValue:@"重新获取验证码"];
        }];
    }];
    
    _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _submitBtn.layer.cornerRadius = 5;
    _submitBtn.layer.masksToBounds = YES;
    _submitBtn.titleLabel.font = MWFont(14);
    [_submitBtn setTitle:[[MWConfigManager manager] objectForKey:@"define" defaultValue:@"确定"] forState:UIControlStateNormal];
    [_submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_submitBtn setBackgroundImage:[UIImage imageWithColor:MWHexColor(@"#1E1934")] forState:UIControlStateNormal];
    [_submitBtn addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_submitBtn];
}

- (void)layoutPageSubviews {
    [_hintLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(90);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
    }];
    
    [_emailTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hintLB.mas_bottom).offset(30);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
    }];
    
    [_emailLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.emailTF.mas_bottom).offset(7);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.equalTo(@0.5);
    }];
    
    [_smsCodeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.emailLineView.mas_bottom).offset(25);
        make.left.equalTo(self.view).offset(16);
    }];
    
    [_countDownButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-16);
        make.centerY.equalTo(self.smsCodeTF);
    }];
    
    [_smsCodeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.smsCodeTF.mas_bottom).offset(7);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.equalTo(@0.5);
    }];
    
    [_submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.smsCodeLineView.mas_bottom).offset(100);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.equalTo(@44);
    }];
}

@end
